/**
 * 
 */


var memoriesCollection = null;


//Create the JSONStore if not exist, or open if exist.
function initCollection(){
	// Common initialization code goes here
	console.log("in initCollection()");
	if ((WL.Client.getEnvironment() === 'iphone' ||
			WL.Client.getEnvironment() === 'ipad' ||
			WL.Client.getEnvironment() === 'android' ||
			WL.Client.getEnvironment() === 'preview') &&
			typeof cordova !== 'undefined' &&
			typeof WL.JSONStore !== 'undefined' &&
			typeof WLJQ !== 'undefined') {
		
		//Initializes the 'users' collection
		var collectionName = 'memories';
		var searchFields = {
								title:"string",
								description:"string", 
								imgSrc: "string",
								memId : "string",
								memDate: "string",
								memLoc: "string"
						   };

		var win =   function (data) {
			console.log('Collection init successful: ' + data);
			initListOfMemories();
		};
		var options = {onSuccess: win, onFailure: genericFailureCallback};
		memoriesCollection = WL.JSONStore.initCollection(collectionName, searchFields, options);
				
	} //end if	
} //end initCollection


//load all the memories to the listView
function initListOfMemories(){	
		WL.Logger.debug('initListOfMemories()');
		if (!checkColInit(memoriesCollection)) {return;}		
		var options = {onSuccess: loadMemories, onFailure: genericFailureCallback};
		memoriesCollection.findAll(options);	
}

//Add new memory to the JSONStore.
function addMemoryToCollection(item){
	console.log('addMemoryToCollection()');
	var win =   function (data) {
		console.log("Memory added .... ");
		/*TODO*/
	};
	var options = {onSuccess: win, onFailure: genericFailureCallback};
	memoriesCollection.add({title: item.title, description: item.description, imgSrc: item.imgSrc, memId: item.memId, memDate: item.memDate, memLoc: item.memLoc },options);
}

//Remove existing memory from the JSONStore.
function removeMemoryFromCollection(id){
	console.log('removeMemoryFromCollection()');
	var options = {onSuccess: win, onFailure: genericFailureCallback};
	memoriesCollection.remove({memId: id}, options);	
}

//Find memory by memId 
function findMemory(id){
	if (!checkColInit(memoriesCollection)) {return;}
	var query = {memId: id};
	var win =   function (data) {
					try{
						console.log(data);	            
		                displayMemoryPage(data);	
					}catch(e){
						console.log("Error:" + e);
					}
	                
	            };
	var options = {onSuccess: win, onFailure: genericFailureCallback};
	memoriesCollection.find(query, options);
}
			
//Find all the memories in the STORE
function findAllMemories(){
	WL.Logger.debug('findAllMemories()');
	if (!checkColInit(memoriesCollection)) {return;}
	var win =   function (data) {		
		for(var i=0; i<data.length; i++){
			console.log(data[i]);			
		}		
	};
	var options = {onSuccess: win, onFailure: genericFailureCallback};
	memoriesCollection.findAll(options);
}

//Return the count of memories in the store.
function countMemories(){
	console.log('countMemories');
	if (checkUndefOrNull(memoriesCollection)) {
		logMessage('You must init the collection first');
	} else {
		var win =   function (data) {
			logMessage('Documents stored: ' + data);
		};
		var options = {onSuccess: win, onFailure: genericFailureCallback};
		memoriesCollection.count(options);
	}
}
			
//Remove and delete the store.
function destroyCollection(){
	console.log('deleteAllMemories()');
	var win =   function () {
		logMessage('JSONStore was destroyed');
	};
	var options = {onSuccess: win, onFailure: genericFailureCallback};
	WL.JSONStore.destroy(options);
}


//Close the collection.
function closeCollection(){
	console.log('closeCollection()');
	var win =   function () {
		console.log('JSONStore was closed');
	};
	var options = {onSuccess: win, onFailure: genericFailureCallback};
	WL.JSONStore.closeAll(options);
}

	//Generic callback called when there's a failure
function  genericFailureCallback(err) {
		console.log(err + ' Error Message: ' + WL.JSONStore.getErrorMessage(err));
}			
	
/*
 * Validation functions 
 * */
	//Simple function that check if obj is null or undefined
	function checkUndefOrNull(obj) {
		return (typeof obj === 'undefined') || obj === null;
	}

	//Simple function that check if collection is init
	function checkColInit(obj) {
		if ((typeof obj === 'undefined') || obj === null){
			console.log('You must initialize the collection first.');
			return false;
		}else{ return true; }
	}
